#pragma once

#include <list>

#include "mission.h"
#include "basic_setup_mission.h"
#include "basic_manage_mission.h"
#include "params.h"

class basic_root_mission : public mission
{
public:
	basic_root_mission(mission *parent_mission, const gtype_list &troop) : mission(parent_mission, troop) 
	{
	}

	virtual void execute()
	{
		LOG << this->get_type() << " executing"  << endl;
		UserAPI &api = mission_manager::get_intance()->get_api();

		basic_setup_mission *bsm = new basic_setup_mission(this, m_troop);
		add_subtask(bsm);
		bsm->execute();
	}

	virtual void event_arrived(mission_event *e)
	{
		LOG << this->get_type() << " event_arrived"  << endl;
		UserAPI &api = mission_manager::get_intance()->get_api();

		if (e->get_type() == "free_general_event")
		{
			gtype_list gl;
			GeneralType general = ((free_general_event*)e)->m_general;
			gl.push_back(general);
			LandCity* city = api.GetCity(api.get_my_army(general)->pos);
			if (city)
			{
				basic_manage_mission *bmm = new basic_manage_mission(this, gl, api.get_my_army(general)->pos);
				add_subtask(bmm);
				bmm->execute();
			} else
			{
				LOG << "Warning: free general at non-city position" << endl;

				//	TODO	general leak
			}
			return;
		}

		LOG << "unhandled event " << e->get_type();
		abort();
	}

	virtual void cancel()
	{
	}

	virtual std::string get_type() { return "basic_root_mission"; };
};
